from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base

from init.mysql import engine

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'
    userId = Column(Integer, name='user_id', primary_key=True)
    username = Column(String(20))
    password = Column(String(50))
    realName = Column(String(20), name="real_name")
    avatar = Column(String(50))
    desc = Column(String(100))
    token = Column(String(50))
    homePath = Column(String(50))

    def __init__(self, user_id, username, password, real_name, avatar, desc, token, home_path):
        self.userId = user_id
        self.username = username
        self.password = password
        self.realName = real_name
        self.avatar = avatar
        self.desc = desc
        self.token = token
        self.homePath = home_path

    @staticmethod
    def login_username_pwd(session, username, password):
        return session.query(User).filter_by(username=username, password=password).first()

    @staticmethod
    def get_user_by_id(session, uid):
        return session.query(User).filter_by(userId=uid).first()


Base.metadata.create_all(engine)
